import ajax from '../libs/ajax.js';
import tools from '../libs/tools.js';

let tbUser = {};
let tbUserInfo = {};
let userOtherInfo = {};
function yhcx() {
  ajax.send('/user/auth/getUserInfo', {}, (data) => {
    if (data.success) {
      tbUser = data.tbUser;
      tbUserInfo = data.tbUserInfo;
      userOtherInfo = data.userOtherInfo;
      yhzs();
    } else {
      showToast('用户未登录');
    }
  });
}
function yhzs() {
  let imgLogo1 = document.getElementById('imgLogo1');
  imgLogo1.src = `${tbUserInfo.img}`;

  let imgLogo2 = document.getElementById('imgLogo2');
  imgLogo2.src = `${tbUserInfo.img}`;

  let imgLogo3 = document.getElementById('imgLogo3');
  imgLogo3.src = `${tbUserInfo.img}`;

  let mc1 = document.getElementById('mc1');
  mc1.innerHTML = tbUser.nickname;
}
//#region 关注我的人
//关注我的人
let tbdata = document.getElementById('tbdata'); 
let szz = [];
function gzwd() {
  let cs = {
    username: tbUser.username
  };
  let cs6 = {
    pageNumber: 1,
    pageSize: 4
  };
  ajax.send(
    '/message/queryFollowMeUserList',
    tools.concatJson(cs, cs6),
    (data) => {
      if (data.success) {
        szz = data.list;
        cs6 = data.page;
        zsym();
      }
    }
  );
}

function zsym(){
  console.log('zxxcscdsvdsvs')
  for (let i = 0; i < szz.length; i++) {
    console.log('zzzzzzzzzxxx')
    const x = szz[i];
    let tr=document.createElement('tr');
    let td;
    //用户头像
    td=document.createElement('img');
    td.classList.add('avator');
    td.setAttribute('src', x.userInfo.img);
    tr.append(td);
    
    tbdata.append(tr);
    //用户名
    td = document.createElement('td');
    td.append(x.user.nickname);
    tr.append(td);
    tbdata.append(tr);
  }
}

//#endregion
//#region 消息

let cs1 = {
  info: ''
};
let page = {
  pageNumber: 1,
  pageSize: 5
};
let sz = [];
function clhyxx() {
  ajax.send('/user/auth/friendQuery', tools.concatJson(cs1, page), (data) => {
    if (data.success) {
      sz = data.list;
      page = data.page;
      zs();
    }
  });
}
console.log('zzzz', sz);
function zs() {
  let sqcl = document.getElementById('sqcl');
  if (sz.newMessage > 0) {
    sqcl.innerHTML = sz.newMessage + '消息';
  }
}
sqcl.addEventListener('click', () => {
  location.href = '../haoyou/huoqyh.html';
});
clhyxx();
//#endregion

//#region 首页
let sy = document.getElementById('sy');
sy.addEventListener('click', () => {
  location.href = './twtwz.html';
});
//#endregion

//#region 发布动态
let fbdttt = document.getElementById('fbdttt');
fbdttt.addEventListener('click', () => {
  location.href = './pr.html';
});
//#endregion

//#region 安全退出
let spExit = document.getElementById('spExit');

spExit.addEventListener('click', () => {
  ajax.send('/user/auth/logout', {}, () => {
    location.href = './twt.html';
  });
});

//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

//#region 发表动态
let fbdt = document.getElementById('fbdt');
fbdt.addEventListener('click', () => {
  location.href = './pr.html';
});
//#endregion

//#region 邮箱修改
let txtEmail = document.getElementById('txtEmail');
let txtEmailCode = document.getElementById('txtEmailCode');
let btnEmailCode = document.getElementById('btnEmailCode');
let btnEmail = document.getElementById('btnEmail');

// 获取邮箱验证码
btnEmailCode.addEventListener('click', () => {
  let info = {
    email: txtEmail.value
  };
  ajax.send('/tool/sendEmailCode', info, (data) => {
    showToast(data.message);
  });
});

// 修改邮箱
btnEmail.addEventListener('click', () => {
  let info = {
    email: txtEmail.value,
    code: txtEmailCode.value
  };
  ajax.send('/user/auth/updateUserEmail', info, (data) => {
    showToast(data.message);
    if (data.success) {
      queryUserInfo();
    }
  });
});

//#endregion

//#region 手机修改
let txtphone = document.getElementById('txtphone');
let txtcode = document.getElementById('txtcode');
let btncode = document.getElementById('btncode');
let btnbd = document.getElementById('btnbd');
let txtimageCode = document.getElementById('txtimageCode');
let imageCode = document.getElementById('imageCode');

btncode.addEventListener('click', () => {
  ajax.send(
    '/tool/sendValidateCode',
    {
      phone: txtphone.value,
      imageCode: txtimageCode.value
    },
    (data) => {
      showToast(data.message);
    }
  );
});

btnbd.addEventListener('click', () => {
  if (txtcode.value.trim() == '') {
    showToast('验证码不能为空');
    return;
  }

  let cs = {
    code: txtcode.value,
    phone: txtphone.value
  };

  ajax.send('/user/auth/updateUserPhone', cs, (data) => {
    showToast(data.message);
    if (data.success) {
      queryUserInfo();
    }
  });
});
//#endregion
//#region 获取图片验证码
//通过ajax获取图片验证码的function

function loadImgCode() {
  ajax.send('/tool/getImageCode', {}, (info) => {
    imageCode.setAttribute('src', info.message);
  });
}

imageCode.addEventListener('click', loadImgCode);

loadImgCode();

btncode.addEventListener('click', () => {
  //验证图片验证码
  ajax.send(
    '/tool/checkImageCode',
    {
      code: txtimageCode.value
    },
    (info) => {
      alert(info.message);
      //不论正确与否，验证码只能使用1次
      //必须重新更新,验证码不区分大小写
      loadImgCode();
    }
  );
});

//#endregion

//#region 好友
let hy1=document.getElementById('hy1');
hy1.addEventListener('click',()=>{
  location.href='../haoyou/haoyzs.html'
})
//#endregion
gzwd();
yhcx();
